package br.com.foxline.atlasmobile.adapter;

import java.io.File;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import br.com.foxline.atlasmobile.dao.AnnexDao;
import br.com.foxline.atlasmobile.dao.ImovelDao;
import br.com.foxline.atlasmobile.entities.Annex;
import br.com.foxline.atlasmobile.entities.Imovel;

public class ImageAdapter extends BaseAdapter {

	private String ALBUMNAME = "atlasmobile";
	private static String PASTE_NAME = "images";

	private Context mContext;

	public Uri[] mThumbUri = {};

	private int mActionBarHeight = 0;

	private Imovel imovel;
	private AnnexDao annexDao;

	private ProgressDialog dialog;

	public ImageAdapter(Context c) {
		mContext = c;
	}

	public ImageAdapter(Context c, Imovel imovel) {
		mContext = c;
		this.imovel = imovel;
		annexDao = new AnnexDao(mContext);
		PASTE_NAME = "imovel-" + this.imovel.getId() + "" + File.separator;
		
		resgateFile();
	}

	public Uri[] getmThumbUri() {
		return mThumbUri;
	}

	public void setmThumbUri(Uri[] mThumbUri) {
		this.mThumbUri = mThumbUri;
	}

	@Override
	public int getCount() {
		return mThumbUri.length;
	}

	@Override
	public Uri getItem(int position) {
		return mThumbUri[position];
	}

	public Long getImage(int position) {
		int cont = 1;
		ImovelDao imoveldao = new ImovelDao(mContext);
		imovel = imoveldao.find(imovel.getId());
		for (Annex a : imovel.getAnnex()) {
			if (cont == position) {
				return a.getId();
			}
		}

		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ImageView imageView = new ImageView(mContext);

		imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

		TypedValue tv = new TypedValue();
		if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize,
				tv, true)) {
			mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
					mContext.getResources().getDisplayMetrics());
		}
		

		ImageLoadTask task = new ImageLoadTask(imageView, 100, 100, false);
		task.execute(mThumbUri[position]);

		return imageView;
	}

	public File getAlbumStorageDir() {
		File file = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
						+ "/" + ALBUMNAME + "/" + PASTE_NAME + "/");

		return file;
	}

	public void resgateFileFinaly() {
		mThumbUri = new Uri[0];

		if (imovel.getAnnex() != null) {
			ImovelDao imoveldao = new ImovelDao(mContext);
			imovel = imoveldao.find(imovel.getId());

			mThumbUri = new Uri[imovel.getAnnex().size()];
			int i = 0;
			for (Annex annex : imovel.getAnnex()) {
				mThumbUri[i] = Uri.parse(annex.getUri());
				i++;
			}
		}
	}

	public void resgateFile() {
		System.err.println("RESGATANDO");
		mThumbUri = annexDao.getUris2((Long) imovel.getId());
		System.err.println("RESGATE FEITO");
	}

	public void reloadResponse() {
		resgateFile();
	}

	public void reloadQuestionnaire() {
		resgateFile();
	}
}
